package downloader

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

func Fetch(url string) ([]byte, error) {
	client := &http.Client{}
	url = strings.Replace(url, "http://", "https://", 1)
	request, err := http.NewRequest("GET", url, nil)
	//增加header选项

	request.Header.Add("Cookie", "sid=95f94dfd-a2bb-4367-96e5-5c4ea3916fee; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1665380824; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1665388268; ec=14sak1yr-1665388267813-51c34eeacc289-1579279952; _exid=9Itge228P2ISrD5aBlAdOQ5QH5elYEVEEdC50PPYGyzp6TB9QzAl1oINidAILVhNFr+i+rHapDkQHwv2HtFWhw==; _efmdata=LNLC1MWKHA9tkO5I4MLBDjnHjqy/3bIx/83b9OaF0Z9x/aRUkVwEThC+V7eX76NnkF7X06tvy9Rm3QLZI2vuL4gW5z6ySlE2PXz+0h8Kn9U=")
	request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")
	// request.Header.Add("X-Requested-With", "xxxx")

	if err != nil {
		log.Fatalf("err %v:", err)
	}
	resp, _ := client.Do(request)
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK { // && resp.StatusCode != http.StatusForbidden
		log.Println()
		// errors.New()  或者 fmt.Errorf()
		return nil, fmt.Errorf("error status code: %d", resp.StatusCode)
	}

	// 会打印请求头 中文乱码
	/*bytes, e := httputil.DumpResponse(resp, true)
	if e != nil{
		log.Println("readBody failed")
	}
	log.Println(string(bytes[:]))*/

	bodyReader := bufio.NewReader(resp.Body) // 包装下
	e := determineEncoding(bodyReader)

	// utf8Reader := transform.NewReader(resp.Body,simplifiedchinese.GBK.NewDecoder())  // 解决中文乱码, 硬编码
	utf8Reader := transform.NewReader(bodyReader, e.NewDecoder()) // 解决中文乱码, 自动判断
	return ioutil.ReadAll(utf8Reader)                             // 传入resp.body
}

// 获取网页编码
// 注意这里必须传bufio.Reader, 不能 io.Reader
func determineEncoding(r *bufio.Reader) encoding.Encoding {
	bytes, e := bufio.NewReader(r).Peek(1024)
	if e != nil {
		log.Printf("Fetcher error: %v", e)
		return unicode.UTF8
	}
	e2, _, _ := charset.DetermineEncoding(bytes, "")
	return e2
}
